package com.blogSystem.article.controller;

import com.blogSystem.article.pojo.Article;
import com.blogSystem.article.pojo.Comment;
import com.blogSystem.article.pojo.CommentThumbs;
import com.blogSystem.article.pojo.Thumbs;
import com.blogSystem.article.service.ArticleService;
import com.blogSystem.article.service.CommentService;
import com.blogSystem.article.service.CommentThumbsService;
import com.blogSystem.article.service.ThumbsSerivlce;
import com.github.pagehelper.PageHelper;
import entity.Result;
import entity.StatusCode;
import io.jsonwebtoken.Claims;
import jdk.net.SocketFlow;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import util.JwtUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private ArticleService articleService;

    @Autowired
    private ThumbsSerivlce thumbsSerivlce;

    @Autowired
    private CommentThumbsService commentThumbsService;

    @Autowired
    private CommentService commentService;

    @Autowired
    private JwtUtil jwtUtil;

    @Autowired
    private HttpServletRequest request;


    @RequestMapping(value = "/addComment",method = RequestMethod.PUT)
    public Result add_comment(@RequestBody Comment comment){

        String token = (String) request.getAttribute("claims_user");
        if (token == null || "".equals(token)) {
            return new Result(false, StatusCode.ACCESSERROR, "权限不足！");
        }
        commentService.addComment(comment);
        return new Result(true, StatusCode.OK,"添加成功！");
    }

    @RequestMapping(value = "/getComment",method = RequestMethod.POST)
    public Result getComment(@RequestParam(required = false, defaultValue = "1") int page, @RequestParam(required = false, defaultValue = "10") int size,String article_id){


        PageHelper.startPage(page,size);
        List<Comment> comments=commentService.findAllParent(article_id);
        String token = (String) request.getAttribute("claims_user");
        Claims claims = jwtUtil.parseJWT(token);
        List<Map> list = new ArrayList<Map>();

        if (token == null || "".equals(token)) {
           for(Comment comment:comments) {
               Map<String, Object> map = new HashMap<String, Object>();
               map.put("comment", comment);
               map.put("thumbs", "null");
               list.add(map);
           }
            return new Result(true, StatusCode.OK, "浏览成功！", list);
        } else {
            String user_id = claims.getId();
            for(Comment comment:comments){
                CommentThumbs commentThumbs=commentThumbsService.findThumbs(comment.getId(),user_id);
                if(commentThumbs!=null){
                    Map<String, Object> map = new HashMap<String, Object>();
                    map.put("comment", comment);
                    map.put("thumbs", "yes");
                    list.add(map);
                }
                else{
                    Map<String, Object> map = new HashMap<String, Object>();
                    map.put("comment", comment);
                    map.put("thumbs", "no");
                    list.add(map);
                }
            }



                return new Result(true, StatusCode.OK, "浏览成功！", list);
            }

        }



    @RequestMapping(value = "getChildComment",method = RequestMethod.POST)
    public Result getChildComment(@RequestParam(required = false, defaultValue = "1") int page, @RequestParam(required = false, defaultValue = "10") int size,String parent_id){


        PageHelper.startPage(page,size);
        List<Comment> comments=commentService.findChildComment(parent_id);
        String token = (String) request.getAttribute("claims_user");
        Claims claims = jwtUtil.parseJWT(token);
        List<Map> list = new ArrayList<Map>();

        if (token == null || "".equals(token)) {
            for(Comment comment:comments) {
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("comment", comment);
                map.put("thumbs", "null");
                list.add(map);
            }
            return new Result(true, StatusCode.OK, "浏览成功！", list);
        } else {
            String user_id = claims.getId();
            for(Comment comment:comments){
                CommentThumbs commentThumbs=commentThumbsService.findThumbs(comment.getId(),user_id);
                if(commentThumbs!=null){
                    Map<String, Object> map = new HashMap<String, Object>();
                    map.put("comment", comment);
                    map.put("thumbs", "yes");
                    list.add(map);
                }
                else{
                    Map<String, Object> map = new HashMap<String, Object>();
                    map.put("comment", comment);
                    map.put("thumbs", "no");
                    list.add(map);
                }
            }



            return new Result(true, StatusCode.OK, "浏览成功！", list);
        }
    }


    @RequestMapping(value = "/thumbs_up",method = RequestMethod.PUT)
    public Result thumbs_up(@RequestBody CommentThumbs thumbs){

        String token = (String) request.getAttribute("claims_user");
        Claims claims = jwtUtil.parseJWT(token);

        if (token == null || "".equals(token)) {
            return new Result(false, StatusCode.ACCESSERROR, "用户未登录！");
        }
        else {
            CommentThumbs findThumbs=commentThumbsService.findThumbs(thumbs.getComment_id(),thumbs.getUser_id());
            if(findThumbs!=null){
                return new Result(false, StatusCode.HAS, "重复点赞！");
            }
            commentThumbsService.addCommentThumbs(thumbs);
            return new Result(true, StatusCode.OK, "点赞成功！");
        }


    }

    @RequestMapping(value = "/thumbs_cancel",method = RequestMethod.DELETE)
    public Result thumbs_cancel(@RequestBody CommentThumbs thumbs){
        String token = (String) request.getAttribute("claims_user");
        Claims claims = jwtUtil.parseJWT(token);

        if (token == null || "".equals(token)) {
            return new Result(false, StatusCode.ACCESSERROR, "用户未登录！");
        }
        else {
            CommentThumbs findThumbs=commentThumbsService.findThumbs(thumbs.getComment_id(),thumbs.getUser_id());
            if(findThumbs==null){
                return new Result(false, StatusCode.ERROR, "未点赞！");
            }
            commentThumbsService.deleteCommentThumbs(thumbs.getUser_id(),thumbs.getComment_id());

            return new Result(true, StatusCode.OK, "取消点赞成功！");
        }
    }




}
